﻿using HungryCrowdLibrary.Data;
using HungryCrowdLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace HungryCrowdLibrary.Manager
{
    public class CategoryManager
    {

        public bool SaveChanges(Category category)
        {
            var success = false;
            using (var hctx = new HungryCrowdEntities())
            {
                hctx.Categories.Add(category);
                success = hctx.SaveChanges() > 0;
            }
            return success;
        }

        public List<ModelDto.Category> GetDtoList(Expression<Func<Category, bool>> search)
        {
            var categories = GetList(search);
            var query = from c in categories
                        select new ModelDto.Category{ Id = c.Id,
                                                      Name = c.Name,
                                                      Synopsis = c.Synopsis,
                                                      IsActive = c.IsActive,
                                                      ImageUrl = c.ImageUrl
                                                    };
            return query.ToList();
        }

        public List<Category> GetList(Expression<Func<Category, bool>> search)
        {
            var categories = new List<Category>();
            using (var hctx = new HungryCrowdEntities())
            {
                categories = hctx.Categories.Where(search).ToList<Category>();
            }
            return categories;
        }
    }
}
